package com.gzhu.knowledgeAdmin.controller;

import com.alibaba.excel.EasyExcel;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.entity.THmdT;
import com.gzhu.knowledgeAdmin.entity.TRwmxT;
import com.gzhu.knowledgeAdmin.service.BillService;
import com.gzhu.knowledgeAdmin.service.DailyBillService;
import com.gzhu.knowledgeAdmin.service.TRwmxTService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/detail")
public class TRwmxTController {

    @Resource
    private TRwmxTService tRwmxTService;

    @PostMapping("/findById/{fRwid}")
    public R find(@PathVariable Long fRwid)
    {
        return R.ok(tRwmxTService.findById(fRwid));
    }

    @PostMapping("/del/{fId}")
    public R deleteById(@PathVariable Long fId)
    {
        return R.ok(tRwmxTService.removeById(fId));
    }

    @PostMapping("/del/batchDelete")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        return R.ok(tRwmxTService.removeByIds(ids));
    }


    @PostMapping("/update")
    public R updateTask(@RequestBody TRwmxT tRwmxT)
    {
        return R.ok(tRwmxTService.updateDetail(tRwmxT));
    }

    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            List<TRwmxT> dataList = EasyExcel.read(file.getInputStream())
                    .head(TRwmxT.class)
                    .sheet()
                    .doReadSync();
            tRwmxTService.saveAll(dataList);
            return R.ok("File uploaded and data saved successfully!");
        } catch (IOException e) {
            return R.error(500, "File upload failed: " + e.getMessage());
        }
    }

    @PostMapping("/add")
    public R addTask(@RequestBody TRwmxT tRwmxT)
    {
        return R.ok(tRwmxTService.insertDetail(tRwmxT));
    }

    @PostMapping("/chonghu")
    public R chonghu(@PathVariable Long fRwid)
    {
        return R.ok(tRwmxTService.chonghu(fRwid));
    }

    // 新增方法
    @GetMapping("/findAllWithRwmc")
    public R findAllWithRwmc() {
        return R.ok(tRwmxTService.findAllWithRwmc());
    }
}
